unit udbsettings;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, IBConnection, sqldb, FileUtil, Forms, Controls, Graphics,
  Dialogs, StdCtrls, EditBtn, ButtonPanel, ComCtrls, DBGrids, Buttons,
  sqlscript, db, types;

type

  { TDBSettingsForm }

  TDBSettingsForm = class(TForm)
    Button1: TBitBtn;
    ButtonPanel1: TButtonPanel;
    PasswordEdit: TEdit;
    Label4: TLabel;
    UsernameEdit: TEdit;
    Label3: TLabel;
    LocalDBCheckBox: TCheckBox;
    HostComboBox: TComboBox;
    DataSource1: TDataSource;
    DBFileNameEdit: TFileNameEdit;
    IBConnection1: TIBConnection;
    Label1: TLabel;
    Label2: TLabel;
    PageControl1: TPageControl;
    SQLQuery1: TSQLQuery;
    SQLScript1: TSQLScript;
    SQLTransaction1: TSQLTransaction;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    procedure Button1Click(Sender: TObject);
    procedure HostComboBoxExit(Sender: TObject);
    procedure LocalDBCheckBoxChange(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  DBSettingsForm: TDBSettingsForm;
  isOK : Boolean;
  isDBConnected : Boolean;

implementation
 uses umain;
{$R *.lfm}

 { TDBSettingsForm }

 procedure TDBSettingsForm.OKButtonClick(Sender: TObject);
 begin
   if not isDBConnected then Button1Click(Sender);
   isOK:=true;
 end;

procedure TDBSettingsForm.HostComboBoxExit(Sender: TObject);
var
  List : TStringList;
     s : String;
begin
  //перевірка, чи хост вже був внесений і додавання в список
  s:= Trim(HostComboBox.Text);
  if s<>'' then HostComboBox.Items.Add(s);
    List := TStringList.Create;
    with HostComboBox,List do try Sorted := True;
      AddStrings(Items);  //  - Туда (Push)
      Items.Assign(List); //  - Обратно (Pull)
    finally Free end;
    isDBConnected:=False;
end;

procedure TDBSettingsForm.LocalDBCheckBoxChange(Sender: TObject);
begin
  HostComboBox.Enabled:=not LocalDBCheckBox.Checked;
end;

procedure TDBSettingsForm.Button1Click(Sender: TObject);
begin
  IBConnection1.Connected:=false;
  isDBConnected:=false;
  AdminForm.BitBtn2.Enabled:=false;
  if not LocalDBCheckBox.Checked then IBConnection1.HostName:=HostComboBox.Text;
  IBConnection1.DatabaseName:=DBFileNameEdit.Text;
  IBConnection1.UserName:=UsernameEdit.Text;
  IBConnection1.Password:=PasswordEdit.Text;
  try
  IBConnection1.Connected:=true;
  except
    ShowMessage('Неможливо підключитись до БД! Перевірте настройки');
    //TODO: запит на створення нової БД
    Exit;
  end;
  isDBConnected:=true;
  ShowMessage('БД підключено!');
end;

initialization
isDBConnected:=false;

end.

